package com.zy.project.system.online.controller;

import com.zy.core.common.utils.StringUtils;
import com.zy.core.data.jpa.support.entity.page.Pagination;
import com.zy.core.util.page.LayuiPageResult;
import com.zy.core.util.page.Result;
import com.zy.core.util.servlet.ServletUtils;
import com.zy.core.web.entity.JsonResult;
import com.zy.project.system.online.entity.OnlineSession;
import com.zy.project.system.online.manager.UserOnlineManager;
import com.zy.project.system.permission.controller.SystemPermissionController;
import com.zy.project.system.permission.entity.SystemPermission;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Date;

/**
 * Package: com.zy.project.system.online.controller
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-12-2 13:36
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
@Controller
@RequestMapping("/system/online")
//@Api("在线信息接口")
public class OnlineController {
    private static final Logger logger = LoggerFactory.getLogger(OnlineController.class);

    private static final String BASE_URL = "system/online/";

    @Autowired
    private UserOnlineManager onlineManager;

    /**
     * 首页
     * @param request
     * @param response
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap model){
        return BASE_URL + "index";
    }

    @GetMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                       String[] ids){
        Result result = new Result();
        try {
            onlineManager.deleteByIds(ids);
            result.suc();
        } catch (Exception e) {
            logger.error("删除出现异常!",e);
            result.error("删除出现异常!");
        }finally {
            ServletUtils.renderJson(response,result.readerJson());
        }
    }

    @GetMapping
    public void list(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                     @RequestParam(value = "page", required = true) int page,
                     @RequestParam(value = "limit", required = true) int limit){
        LayuiPageResult result = new LayuiPageResult();
        try {
            Pagination<OnlineSession> pageBean = onlineManager.findAllOnlineSession(page, limit);
            result.init(pageBean);
        } catch (Exception e) {
            logger.error("获取列表信息失败",e);
        } finally {
            ServletUtils.renderJson(response,result.readerJson());
        }
    }

}
